package com.sihai.ehr.web.action.salary;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.SalaryCustom;
import com.sihai.ehr.model.biz.SalaryCustomDetail;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.SalaryFrameworkEnum;
import com.sihai.ehr.model.sys.SalaryStructureEnum;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.SalaryCustomService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 薪资结构控制器
 * 
 * @author henry
 * 
 */
@Results({ @Result(name = "custom_user_setting", location = "custom_user_setting.jsp"),
		@Result(name = "list", location = "custom_user_list.jsp") })
public class CustomUserSettingAction extends EHRAction {
	private Log log = LogFactory.getLog(CustomUserSettingAction.class);

	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private SalaryCustomService salaryCustomService;

	private LimitedList<EmployeeinfoWork> works;
	private List<Organization> organizations;

	private SalaryCustom custom;
	private LimitedList<SalaryCustom> customs;
	private Long customId;

    private String listType;
    private String result;
	
	@Action("/salary/custom_user_setting")
	@Override
	public String execute() throws Exception {
        organizations = organizationService.findChildren(organizationId, false, null, null);
		return "custom_user_setting";
	}

    //未设置员工列表
	@Action("/salary/custom_user_list_un_set")
	public String unSetList()throws Exception{
        Long orgId = NumberUtils.createLong(getParameter("orgId"));
        Long dutyId = NumberUtils.createLong(getParameter("dutyId"));
        Long gradeId = NumberUtils.createLong(getParameter("gradeId"));
        Long gradeLevelId = NumberUtils.createLong(getParameter("gradeLevelId"));
        Float workYearStart = null;
        Float workYearEnd = null;
        //Float workYearStart = NumberUtils.createFloat(getParameter("workYearStart"));
        //Float workYearEnd = NumberUtils.createFloat(getParameter("workYearEnd"));
        works = salaryCustomService.findCustomUnSetUsers(customId, organizationId, orgId, dutyId, gradeId, gradeLevelId, workYearStart, workYearEnd, createLimit(), createSorter());
        this.setPageUrl("/salary/custom_user_list_un_set.xhtml");
        paging(works);
        listType = "un_set";
        return "list";
	}

    //已设置员工列表
    @Action("/salary/custom_user_list_has_set")
    public String hasSetList()throws Exception{
        works = salaryCustomService.findCustomHasSetUsers(customId, createLimit(), createSorter());
        this.setPageUrl("/salary/custom_user_list_has_set.xhtml");
        paging(works);
        listType = "has_set";
        return "list";
    }

    @Action(value = "/salary/custom_user_add", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String addUser()throws Exception{
        String[] workIds = StringUtils.split(getParameter("add_work_ids"), ",");
        if(workIds == null || workIds.length == 0){
            result = "false";
            return ERROR;
        }
        salaryCustomService.addUserToCustom(customId, workIds);
        result = "true";
        return SUCCESS;
    }
    @Action(value = "/salary/custom_user_delete", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String deleteUser()throws Exception{
        String workIds = getParameter("add_work_ids");
        if(StringUtils.isBlank(workIds)){
            result = "false";
            return ERROR;
        }
        salaryCustomService.deleteUserFromCustom(customId, workIds);
        result = "true";
        return SUCCESS;
    }


    public LimitedList<EmployeeinfoWork> getWorks() {
        return works;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public SalaryCustom getCustom() {
		return custom;
	}
	
	public void setCustom(SalaryCustom custom) {
		this.custom = custom;
	}
	
	public LimitedList<SalaryCustom> getCustoms() {
		return customs;
	}
	
	public void setCustoms(LimitedList<SalaryCustom> customs) {
		this.customs = customs;
	}
	
	public Long getCustomId() {
		return customId;
	}
	
	public void setCustomId(Long customId) {
		this.customId = customId;
	}

    public String getListType() {
        return listType;
    }

    public String getResult() {
        return result;
    }
}
